package com.jinzhending.service.wx.impl;

import com.alibaba.fastjson.JSONObject;
import com.jinzhending.comm.MoneyConvert;
import com.jinzhending.dao.wx.UserDao;
import com.jinzhending.entity.GoodsInfo;
import com.jinzhending.entity.WxUser;
import com.jinzhending.service.wx.UserService;
import com.jinzhending.utils.AESUtil;
import com.jinzhending.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ProjectName: LieIdle
 * @Package: com.jinzhending.service.wx.impl
 * @Author: huat
 * @Date: 2020/4/9 14:43
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    /**
     * 通过用户名密码查询用户是否存在
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @Override
    public WxUser getWxUser(String username, String password) {
        WxUser wxUser=userDao.getWxUser(username);
        if(null==wxUser){
            return null;
        }
        if( MD5Util.encode(password).equals(wxUser.getPassword())){
            List<GoodsInfo> goodList=wxUser.getGoodsInfoList();
            for (GoodsInfo goodsInfo:goodList){
                goodsInfo.setGoodsMoneyNumber(MoneyConvert.fen2YuanStr(goodsInfo.getGoodsMoneyNumber()));
            }
            return wxUser;

        }
      return null;
    }

    @Override
    public int updateTokenById(int id, String token) {
        return userDao.updateTokenById(id, token);
    }

    @Override
    public int updatePassword(int id, String password) {

        return  userDao.updatePassword(id, MD5Util.encode(password));
    }
}
